package com.zjpavt.client.artnet;

import com.zjpavt.client.artnet.device.ColorConst;
import com.zjpavt.client.artnet.device.EffectUtil;
import com.zjpavt.client.artnet.device.StripLight;
import com.zjpavt.client.artnet.effect.IEffect;
import com.zjpavt.client.artnet.effect.SameColorEffect;
import com.zjpavt.client.artnet.packets.ArtDmxPacket;
import com.zjpavt.client.util.ClientConfig;
import com.zjpavt.client.util.PropertiesUtil;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    /*
        private static int base = 0;
        private static final ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(1);
        private static int index;
        private static String stripLight = "StripLight";

        public static void main(String[] args) {
            show();
        }
        private static List<StripLight> lightSystem() {
            List<StripLight> lightList = new ArrayList<>(8);
            for (int i = 1 ; i < 9; i++) {
                try {
                    String lightName = stripLight + i;
                    String lightConfig = PropertiesUtil.getProperties(ClientConfig.ART_NET_CONFIG_PROPERTIES, lightName,null);
                    if (lightConfig != null) {
                        StripLight stripLight = new StripLight(lightConfig);
                        IEffect iEffect = new SameColorEffect(stripLight, ColorConst.WHITE10);
    //                    IEffect iEffect = new WaterWaveEffect(stripLight, ColorConst.RED10);
                        stripLight.setEffect(iEffect);
                        stripLight.setDisplayName(lightName);
                        lightList.add(stripLight);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return lightList;
        }

        private static ArtNetServer initServer() {
            ArtNetServer artNetServer = new ArtNetServer(5444, 6454);
            try {
                String broadcaseAddress = PropertiesUtil.getProperties(ClientConfig.ART_NET_CONFIG_PROPERTIES,"broadcaseAddress",null);
                artNetServer.setBroadcastAddress(broadcaseAddress);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                artNetServer.start();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (ArtNetException e) {
                e.printStackTrace();
            }
            return artNetServer;
        }

        private static void show() {
            Map<Integer, byte[]> dmxDataMap = new HashMap<>();
            List<StripLight> stripLights = lightSystem();
            ArtNetServer artNetServer = initServer();
            for (StripLight s : stripLights) {
                byte[] dmxData = dmxDataMap.getOrDefault(s.getUniverse(), new byte[512]);
                s.getEffect().show();
                dmxData = s.append2DmxData(dmxData);
                dmxDataMap.put(s.getUniverse(), dmxData);
            }
            for (int universeIndex  : dmxDataMap.keySet()             ) {
                byte[] data = dmxDataMap.get(universeIndex);
                ArtDmxPacket artDmxPacket = new ArtDmxPacket();
                artDmxPacket.setDMX(data,data.length);
                artDmxPacket.setUniverse(0, universeIndex - 1);
                artNetServer.broadcastPacket(artDmxPacket);
            }
        }
        private static void test() {
            try {
                String s1 = PropertiesUtil.getProperties("artnet.properties","StripLight1");
                StripLight stripLight = new StripLight(126,39,1);
                System.out.println(s1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ColorConst.tiaoxiang(ColorConst.WHITE10, 0.05);
            ArtNetServer artNetServer = new ArtNetServer(5444, 6454);
            artNetServer.setBroadcastAddress("192.168.0.255");
            //        InetAddress inetAddress = InetAddress.getByName("2.0.0.169");
            //        artNetServer.setBroadcastAddress("172.16.255.255");
            try {
                artNetServer.start();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (ArtNetException e) {
                e.printStackTrace();
            }

            byte[] color = {-1, -1, -1};
            byte[] color2 = {11, -1, 31};
            byte[] color3 = {9, -1, 9};

            StripLight stripLight = new StripLight(126,39,1);
            StripLight stripLight2 = new StripLight(0,42,1);
            StripLight stripLight3 = new StripLight(243,6,1);
            ArtDmxPacket artDmxPacket = new ArtDmxPacket();
            artDmxPacket.setUniverse(0, 0);
            s.scheduleAtFixedRate(() -> {
                byte[] bytes;
                byte[] dmxData = new byte[512];
                index+=8;
                bytes = EffectUtil.waterfallEffect(stripLight3, color, color2, index);
                bytes = EffectUtil.waterfallEffect(stripLight2, color, color2, index);
                bytes = EffectUtil.waterfallEffect(stripLight, ColorConst.tiaoxiang(ColorConst.WHITE10,0.05), ColorConst.BLACK,index,38,1);
                EffectUtil.waterfallSin(stripLight, ColorConst.tiaoxiang(ColorConst.BLUE10,0.1), index, 30, 0);
                stripLight.append2DmxData(dmxData);
                stripLight2.append2DmxData(dmxData);
        //            stripLight3.append2DmxData(dmxData);
                artDmxPacket.setDMX(dmxData, dmxData.length);
                artNetServer.broadcastPacket(artDmxPacket);

            }, 0, 100, TimeUnit.MILLISECONDS);
        }

        private static byte format(int data) {
            return (byte) (data >> 8 & 0xff);
        }
    */
}
